package com.longZ.himalaya.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.longZ.himalaya.DetailActivity;
import com.longZ.himalaya.R;
import com.longZ.himalaya.adapters.RecommendListAdapter;
import com.longZ.himalaya.base.BaseFragment;
import com.longZ.himalaya.interfaces.IRecommendViewCallback;
import com.longZ.himalaya.presenters.AlbumDetailPresenter;
import com.longZ.himalaya.presenters.RecommendPresenter;
import com.longZ.himalaya.views.UILoader;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

/**
 * 创建者 by:陈泰龙
 * <p>
 * 2020/7/1
 **/

public class RecommendFragment extends BaseFragment implements IRecommendViewCallback, UILoader.onRetryClickListener, RecommendListAdapter.OnRecommendItemClickListener {
    private static final String TAG = "RecommendFragment";
    private View mRootView;
    private RecyclerView mRecommendRV;
    private RecommendListAdapter mRecommendListAdapter;
    private RecommendPresenter mRecommendPresenter;
    private UILoader mUILoader;


    @Override
    protected View onSubViewLoaded(final LayoutInflater inflater, ViewGroup container) {
        mUILoader = new UILoader(getContext()) {
            @Override
            protected View getSuccessView(ViewGroup container) {
                return createSuccessView(inflater,container);
            }
        };
        mRecommendPresenter = RecommendPresenter.getInstance();   //获取到逻辑层的对象
        mRecommendPresenter.registerViewCallback(this);   //先要设置通知接口的注册
        mRecommendPresenter.getRecommendList();     //获取推荐列表
        if (mUILoader.getParent() instanceof ViewGroup) {
            ((ViewGroup) mUILoader.getParent()).removeView(mUILoader);
        }
        mUILoader.setOnRetryClickListener(this);
        return mUILoader;                        //返回View
    }

    private View createSuccessView(LayoutInflater inflater, ViewGroup container) {
        mRootView = inflater.inflate(R.layout.fragment_recommend, container, false); //view加载完成
        mRecommendRV = mRootView.findViewById(R.id.recommend_fragment); //RecyclerView的使用
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext()); //设置布局管理器
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);//设置方向
        mRecommendRV.setLayoutManager(linearLayoutManager);
        mRecommendRV.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.top = UIUtil.dip2px(view.getContext(), 5);
                outRect.bottom = UIUtil.dip2px(view.getContext(), 5);
                outRect.left = UIUtil.dip2px(view.getContext(), 10);
                outRect.right = UIUtil.dip2px(view.getContext(), 10);
            }
        });
        /**
         * 设置适配器
         */
        mRecommendListAdapter = new RecommendListAdapter();
        mRecommendRV.setAdapter(mRecommendListAdapter);
        mRecommendListAdapter.setOnRecommendItemClickListener(this);
        return mRootView;
    }

    /**
     * 当我们获取到推荐内容时，这个方法就会被调用（成功）
     * 把数据设置给适配器，并且更新UI
     *
     * @param result
     */
    @Override
    public void onRecommendListLoaded(List<Album> result) {
        mRecommendListAdapter.setData(result);
        mUILoader.updateStatus(UILoader.UIStatus.SUCCESS);
    }

    @Override
    public void onNetworkError() {
        mUILoader.updateStatus(UILoader.UIStatus.NETWORK_ERROR);
    }

    @Override
    public void onEmpty() {
        mUILoader.updateStatus(UILoader.UIStatus.EMPTY);
    }

    @Override
    public void onLoading() {
        mUILoader.updateStatus(UILoader.UIStatus.LOADING);
    }


    /**
     * 取消接口的注册,避免内存泄漏
     */
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mRecommendPresenter != null) {
            mRecommendPresenter.unRegisterViewCallback(this);
        }
    }

    @Override
    public void onRetryClick() {
        //表示网络不佳的时候，用户点击了重试
        //重新获取数据接口即可
        if (mRecommendPresenter != null) {
            mRecommendPresenter.getRecommendList();
        }
    }

    @Override
    public void onItemClick(int position, Album album) {
        AlbumDetailPresenter.getInstance().setTargetAlbum(album);
        // item被点击了,跳转到详情界面
        Intent intent= new Intent(getContext(), DetailActivity.class);
        startActivity(intent);
    }
}
